package com.cx.web;

import com.cx.dto.CustomerDto;
import com.cx.entity.Customer;
import com.cx.service.CustomerService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: 舒建辉
 * @Description:
 * @Date: Created on 2018/2/1
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController<CustomerController> {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/index")
    public String index() {
        Date now = new Date();
        customerService.save(new Customer((int)Math.random()+(char)(int)(Math.random()*26+97)+ "" +(char)(int)(Math.random()*26+97), (char)(int)(Math.random()*26+97)+""+(int)Math.random(), new Random().nextInt(3)+1, (char)(int)(Math.random()*26+97)+(char)(int)(Math.random()*26+97)+(int)Math.random() + "@cx.com", now));
        customerService.save(new Customer((char)(int)(Math.random()*26+97)+ "" +(char)(int)(Math.random()*26+97)+(int)Math.random(), (char)(int)(Math.random()*26+97)+""+(int)Math.random(), new Random().nextInt(3)+1, (char)(int)(Math.random()*26+97)+(char)(int)(Math.random()*26+97)+(int)Math.random() + "@cx.com", now));
        customerService.save(new Customer((int)Math.random()+ "" +(int)Math.random()+(char)(int)(Math.random()*26+97), (char)(int)(Math.random()*26+97)+""+(int)Math.random(), new Random().nextInt(3)+1, (char)(int)(Math.random()*26+97)+(char)(int)(Math.random()*26+97)+(int)Math.random() + "@cx.com", now));
        customerService.save(new Customer((char)(int)(Math.random()*26+97)+ "" +(int)Math.random()+(int)Math.random(), (char)(int)(Math.random()*26+97)+""+(int)Math.random(), new Random().nextInt(3)+1, (char)(int)(Math.random()*26+97)+(char)(int)(Math.random()*26+97)+(int)Math.random() + "@cx.com", now));
        customerService.save(new Customer((char)(int)(Math.random()*26+97)+ "" +(char)(int)(Math.random()*26+97)+(int)Math.random(), (char)(int)(Math.random()*26+97)+""+(int)Math.random(), new Random().nextInt(3)+1, (char)(int)(Math.random()*26+97)+(char)(int)(Math.random()*26+97)+(int)Math.random() + "@cx.com", now));
        return "ok";
    }

    @RequestMapping("/update")
    public String update() {
        List<Customer> customers = findAll();
        if(null != customers && !customers.isEmpty()) {
            Customer customer = customers.get(0);
            customer.setName((int) Math.random() + (char) (int) (Math.random() * 26 + 97) + "" + (char) (int) (Math.random() * 26 + 97));
            customerService.save(customer);
        }
        return "ok";
    }

    @RequestMapping("/batchUpdate")
    public String batchUpdate() {
        List<Customer> customers = findAll();
        customers.stream().filter(p-> null != p).forEach(customer -> {
            customer.setName((int) Math.random() + (char) (int) (Math.random() * 26 + 97) + "" + (char) (int) (Math.random() * 26 + 97));
        });
        customerService.save(customers);
        return "ok";
    }

    @RequestMapping("/save")
    public String save(@RequestBody Customer customer) {
        customerService.save(customer);
        return "ok";
    }

    @RequestMapping("/batchSave")
    public String batchSave(Integer maxRow) {
        long t1 = System.currentTimeMillis();
        log.info("===============================================> " + t1);
        List<Customer>  customers = buildCustomers(maxRow);
        customerService.save(customers);
        long t2 = System.currentTimeMillis();
        log.info("===============================================> " + (t2 - t1));
        return "ok";
    }

    private List<Customer> buildCustomers(int max){
        List<Customer> customers = new ArrayList<>(10000);
        for (int i = 1; i <= max; i++) {
            customers.add(buildCustomer(i));
        }
        return customers;
    }
    private Customer buildCustomer(int i){
        String iStr = i > 0 ? ""+i : "";
        Customer customer = new Customer();
        customer.setName("曹操" + iStr);
        customer.setSurname("曹操ddd" + iStr);
        customer.setSex(new Random().nextInt(3)+1);
        customer.setEmail("wsy@qq.com");
        customer.setCreatedTime(new Date());
        return customer;
    }

    @RequestMapping("/findAll")
    public List<Customer> findAll() {
        String apikey = apikey("findAll", null, null);
//        List<Customer> customers = getList1stCache(apikey, Customer.class);
//        if(!CollectionUtils.isEmpty(customers)) {
//            return customers;
//        }

        List<Customer> customers = customerService.findAll();
//        putList1stCache(apikey, customers);
        return CollectionUtils.isEmpty(customers)?Lists.newArrayListWithCapacity(0):customers;
    }

    @RequestMapping("/findById/{id}")
    public Customer findById(@PathVariable Long id) {
        Customer customer = customerService.findById(id);
        return customer;
    }

    @RequestMapping("/findBySex/{sex}")
    public List<Customer> findBySex(@PathVariable Integer sex) {
        String apikey = apikey("findBySex", new String[]{"sex"}, new Object[]{sex});
//        List<Customer> customers = getList1stCache(apikey, Customer.class);
//        if(!CollectionUtils.isEmpty(customers)) {
//            return customers;
//        }

        List<Customer> customers = customerService.findBySex(sex);
//        putList1stCache(apikey, customers);
        return CollectionUtils.isEmpty(customers)?Lists.newArrayListWithCapacity(0):customers;
    }

    @RequestMapping("/findByName/{name}")
    public List<Customer> findByName(@PathVariable String name) {
        String apikey = apikey("findByName", new String[]{"name"}, new Object[]{name});
//        List<Customer> customers = getList1stCache(apikey, Customer.class);
//        if(!CollectionUtils.isEmpty(customers)) {
//            return customers;
//        }

        List<Customer> customers = customerService.findByName(name);
//        putList1stCache(apikey, customers);
        return CollectionUtils.isEmpty(customers)?Lists.newArrayListWithCapacity(0):customers;
    }

    @RequestMapping("/findByNameOrSurname/{name}/{surname}")
    public List<Customer> findByNameOrSurname(@PathVariable String name, @PathVariable String surname) {
        String apikey = apikey("findByNameOrSurname", new String[]{"name", "surname"}, new Object[]{name, surname});
//        List<Customer> customers = getList1stCache(apikey, Customer.class);
//        if(!CollectionUtils.isEmpty(customers)) {
//            return customers;
//        }

        List<Customer> customers = customerService.findByNameOrSurname(name, surname);
//        putList1stCache(apikey, customers);
        return CollectionUtils.isEmpty(customers)?Lists.newArrayListWithCapacity(0):customers;
    }

    @RequestMapping("/delete/{id}")
    public Integer delete(@PathVariable Long id) {
        customerService.delete(id);
        return 1;
    }

    @RequestMapping("/findByCustomerId")
    public CustomerDto findByCustomerId() {
        Page<Customer> page = customerService.findPageBySex(1,1,3);
        return customerService.findByCustomerId(page.getContent().get(0).getId());
    }

    @RequestMapping("/findByTaskIds")
    public List<CustomerDto> findByTaskIds() {
        List<Long> ids = Lists.newArrayListWithCapacity(4);
        Page<Customer> page = customerService.findPageBySex(1,4,3);
        page.getContent().iterator().forEachRemaining(customer -> {
            ids.add(customer.getId());
        });

        return customerService.findByTaskIds(ids);
    }

    @RequestMapping("/findByCustomerIds")
    public List<Long> findByCustomerIds() {
        List<Long> ids = Lists.newArrayListWithCapacity(4);
        Page<Customer> page = customerService.findPageBySex(1,4,3);
        page.getContent().iterator().forEachRemaining(customer -> {
            ids.add(customer.getId());
        });

        return customerService.findByCustomerIds(ids);
    }

    /**
     * 查找
     * @return
     */
    @RequestMapping("/findCustomerDtoByIds")
    public List<CustomerDto> findCustomerDtoByIds() {
        List<Long> ids = Lists.newArrayListWithCapacity(4);
        Page<Customer> page = customerService.findPageBySex(1,4,3);
        page.getContent().iterator().forEachRemaining(customer -> {
            ids.add(customer.getId());
        });

        return customerService.findCustomerDtoByIds(ids);
    }

    @RequestMapping("/findPage")
    public Page<Customer> findPage(@RequestParam Integer page, @RequestParam Integer size){
        return customerService.findPage(page,size);
    }

    @RequestMapping("/findPageSort")
    public Page<Customer> findPageSort(@RequestParam Integer page, @RequestParam Integer size){
        return customerService.findPageSortById(page,size);
    }


    @RequestMapping("/findBySort")
    public List<Customer> findBySort(){
        return customerService.findBySortById();
    }

    @RequestMapping("/findPageBySex")
    public Page<Customer> findPageBySex(@RequestParam Integer page, @RequestParam Integer size,@RequestParam Integer sex){
        return customerService.findPageBySex(page,size,sex);
    }

    @RequestMapping("/findBySpecBySex")
    public List<Customer> findBySpecBySex(){
        return customerService.findBySpecBySex();
    }


    @RequestMapping("/findByCustomer")
    public List<Customer> findByCustomer(){
        return customerService.findByCustomer();
    }

    @RequestMapping("/findByCustomerBySort")
    public List<Customer> findByCustomerBySort(){
        return  customerService.findByCustomerBySort();
    }

    @RequestMapping("/findPageByCustomer")
    public Page<Customer> findPageByCustomer(){
        return  customerService.findPageByCustomer();
    }

}
